// pages/card_address/card_address.js
var util = require('../../utils/util.js');
var api = require('../../config/api.js');
var area = require('../../utils/area.js');


Page({

  /**
   * 页面的初始数据
   */
  data: {
    Gzshow: false, //説明
    areaList: area.areaList, //地址数据
    whereshow: false, // 地点选择
    province: '', //省
    city: '', //市
    county: '', //区

    show: false,

    addressid: 0,
    strEmail: ''

  },


  // 服务内容弹出层控制
  GzPopup() {
    this.setData({
      Gzshow: true
    });
  },

  GzClose() {
    this.setData({
      Gzshow: false,

    });
  },

  // 位置弹出层控制
  upwhere() {
    this.setData({
      whereshow: true
    });
  },

  whereClose() {
    this.setData({
      whereshow: false
    });
  },

  // 获取区域
  getWhere(e) {

    this.province = e.detail.values[0].name
    this.city = e.detail.values[1].name
    this.county = e.detail.values[2].name
    this.setData({
      province: this.province,
      city: this.city,
      county: this.county,
    })
    // 关闭窗口
    this.whereClose()
  },
  //获取邮箱
  getEmail(e) {
    var strEmail = e.detail.value
    this.setData({
      strEmail: strEmail
    })
  },

  //查询默认地址
  getDefaltData() {
    let that = this;
    util.request(api.QueDefault).then(function(res) {
      if (res.errno == 0) {
        that.setData({
          addressBean: res.data
        })

      } else {
        util.myToast(res.errmsg)
      }
    });
  },
  //查询地址详情
  getAddressData() {
    let that = this;
    util.request(api.AddressDetail, {
      id: that.data.addressid
    }).then(function(res) {
      if (res.errno == 0) {
        that.setData({
          addressBean: res.data
        })

      } else {
        util.myToast(res.errmsg)
      }
    });
  },
  //选择地址
  onChooseAddress(e) {
    wx.navigateTo({
      url: '/pages/address/address',
    })
  },
  //确认使用接口
  getUseData() {
    let that = this;
    util.request(api.UseGiftOrder, {
      orderId: that.data.orderid,
      addressId: that.data.addressBean.id,
      mail: that.data.strEmail
    },'post').then(function(res) {
      if (res.errno == 0) {
        wx.redirectTo({
          url: '/pages/card_get/card_get',
        })

      } else {
        util.myToast(res.errmsg)
      }
    });
  },
  //确认使用
  onUse(e) {
    if (util.isEmpy(this.data.addressBean.id)) {
      util.myToast('请选择收货地址')
    } else if (util.isEmpy(this.data.strEmail)) {
      util.myToast('请输入电子邮箱')
    } else {
      this.getUseData();
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      money: options.money,
      orderid: options.orderid
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    const pages = getCurrentPages()
    const currPage = pages[pages.length - 1] // 当前页
    var addressid = currPage.data.addressid;
    addressid = util.isEmpy(addressid) ? '0' : addressid;
    console.log("addressid--", addressid)
    this.setData({
      addressid: addressid
    })
    if (addressid == 0) {
      this.getDefaltData();
    } else {
      this.getAddressData();
    }

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})